﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MSBIHelper.Helper;
using System.Diagnostics;

namespace MSBIHelper.UI
{
    public partial class Frm_try : Form
    {
        public Frm_try()
        {
            InitializeComponent();
            //说明
            rtbMsg.AppendText("choose a file or more, get the top or last n rows for you\r\n");
            rtbMsg.AppendText("choose a file or more, count the line count for you \r\n");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            int rowcount = 10;
            if(int.TryParse(tb_rowcount.Text.Trim(),out rowcount))
            {
                if (rowcount == 0)
                {
                    MessageBox.Show("please input a valid number of rows more than zero!");
                }
                else
                {
                    try
                    {
                       string[] files = Util.SelectFiles(false);
                       foreach (var filename in files)
                       {
                           rtbMsg.AppendText(Util.GetRowsOFFile(filename, rowcount, rb_top.Checked ? 0 : 1));
                           rtbMsg.AppendText(Environment.NewLine);
                       }
                    }
                    catch (Exception ex)
                    {
                        rtbMsg.AppendText(ex.Message);
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        { 
            string filename =  DateTime.Now.ToShortDateString().Replace("/","_")+".txt";
            using(System.IO.StreamWriter sw = new System.IO.StreamWriter(filename))
            {
                int rowcount = int.Parse(tb_rowcount.Text);
                int i=1;
                int j = 1;
                do
                {
                    sw.WriteLine(" I love you " + j++.ToString());
                    sw.WriteLine("我爱你中国" + j++.ToString());
                } while (i++ < rowcount);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                string[] files = Util.SelectFiles(false);
                Stopwatch sw = new Stopwatch();
                foreach (var filename in files)
                {
                    sw.Reset();
                    sw.Start();
                    rtbMsg.AppendText(
                        filename + " row cout is :" +
                        Util.CountFileRowCount(filename).ToString()
                    );
                    rtbMsg.AppendText(" consume seconds: " + sw.Elapsed.TotalSeconds);
                    rtbMsg.AppendText(Environment.NewLine);
                }
            }
            catch (Exception ex)
            {
                rtbMsg.AppendText(ex.Message);
            }
        }
    }
}
